package com.sky.utils;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;
@Component

public class UploadFileUtil {
    @Value("${baseFilePath}")
    private String baseFilePath;

    public String uploadFile(MultipartFile file) throws IOException {
        if(file.isEmpty()){
            throw new RuntimeException("上传文件不能为空");
        }
        String originalFileName = file.getOriginalFilename();
        String extension = originalFileName.substring(originalFileName.lastIndexOf("."));
        String newFileName = UUID.randomUUID().toString() + extension;
        // 保存文件
        File dest = new File(baseFilePath + newFileName);
        file.transferTo(dest);
        return baseFilePath + newFileName;
    }
}
